package com.yanqu.road.server.cmd.disperseGold.cross;

import com.yanqu.road.entity.disperseGold.DisperseGoldInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.DisperseGoldPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cross.disperseGold.CrossDisperseGoldProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.DisperseGoldMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.*;

@Cmd(code = Protocol.S_CROSS_DISPERSEGOLD_INFO , desc = "跨服送财信息")
public class CrossDisperseGoldCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CrossDisperseGoldProto.CrossDisperseGoldMsg reqMsg = CrossDisperseGoldProto.CrossDisperseGoldMsg.parseFrom(packet.getBytes());
        DisperseGoldInfo info = DisperseGoldPb.parseToDisperseGoldInfo(reqMsg.getInfo());
        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerInfo());
        info.setUserBaseInfo(userBaseInfo);
        List<Long> serverIds = reqMsg.getServerIdsList();
        if (serverIds.contains(GameServer.getInstance().getServerId())) {
            DisperseGoldMgr.crossDisperseGoldInfo(userBaseInfo, info, true, null);
        } else {
            // 聚合一下
            List<CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp> groupUserTempList = reqMsg.getGroupUserListList();
            Set<Long> userIds = new HashSet<>();
            for (CrossDisperseGoldProto.CrossDisperseGoldGroupUserTemp groupUserTemp : groupUserTempList) {
                if (serverIds.contains(groupUserTemp.getServerId())) {
                    continue;
                }
                // 不是本服的也不处理
                if (groupUserTemp.getServerId() != GameServer.getInstance().getServerId()) {
                    continue;
                }
                userIds.add(groupUserTemp.getUserId());
            }
            DisperseGoldMgr.crossDisperseGoldInfo(userBaseInfo, info, false, userIds);
        }
    }
}
